package org.dbi.junit;

import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Date;

import org.dbi.mysql.Rdbms;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class TestMyDatabasePleunEnWalter {
	private Rdbms myRdbms = null;

	@Before
	public void setUp() throws Exception {
		myRdbms = Rdbms.getInstance();
		
	}

	@After
	public void tearDown() throws Exception {
		myRdbms = null;

	}
	
	@Test
	public void createWholeFietsDB(){
		assertTrue(myRdbms.createDatabase("FietsDB"));
		myRdbms.useDatabase("FietsDB");
		
		assertTrue(myRdbms.createTable("cons1", "Track","FietsDB"));
		assertTrue(myRdbms.createTable("cons2", "KnpTrack","FietsDB"));
		assertTrue(myRdbms.createTable("cons3", "Knooppunt","FietsDB"));
		
		assertTrue(myRdbms.createAttribute("nummer", "FietsDB", "Track", "Integer", true));
		assertTrue(myRdbms.createAttribute("Naam", "FietsDB", "Track", "varchar", true));
		assertTrue(myRdbms.createAttribute("Rel-Date", "FietsDB", "Track", "Date", true));
		
		// Wij ondersteunen geen real daarom integer
		assertTrue(myRdbms.createAttribute("Track", "FietsDB", "KnpTrack", "Integer", true));
		assertTrue(myRdbms.createAttribute("Long", "FietsDB", "KnpTrack", "Integer", true));
		assertTrue(myRdbms.createAttribute("Lat", "FietsDB", "KnpTrack", "Integer", true));
		assertTrue(myRdbms.createAttribute("Vlgnr", "FietsDB", "KnpTrack", "Integer", true));
		
		
		assertTrue(myRdbms.createAttribute("Lat", "FietsDB", "Knooppunt", "Integer", true));	
		assertTrue(myRdbms.createAttribute("Long", "FietsDB", "Knooppunt", "Integer", true));
		assertTrue(myRdbms.createAttribute("Nummer", "FietsDB", "Knooppunt", "Integer", true));
		assertTrue(myRdbms.createAttribute("Type", "FietsDB", "Knooppunt", "varchar", false));
		
	
		ArrayList<Object[]> G1 = new ArrayList<Object[]>();
		Object[] o1= { "nummer", "Integer", true };
		
		ArrayList<Object[]> G2 = new ArrayList<Object[]>();
		Object[] o2= { "Naam", "varchar", true };		
		Object[] o3= { "Rel-Date", "Date", true };
		
		ArrayList<Object[]> G3 = new ArrayList<Object[]>();
		Object[] o4= { "Track", "Integer", true };
		Object[] o5= { "Long", "Integer", true };
		Object[] o6= { "Lat", "Integer", true };
		
		ArrayList<Object[]> G4 = new ArrayList<Object[]>();
		Object[] o7= { "Track", "Integer", true };
	
		
		ArrayList<Object[]> G5 = new ArrayList<Object[]>();
		Object[] o8= { "Long", "Integer", true };
		Object[] o9= { "Lat", "Integer", true };
		
		
		ArrayList<Object[]> G6 = new ArrayList<Object[]>();
		Object[] o10= { "Lat", "Integer", true };
		Object[] o11= { "Long", "Integer", true };		
	
		G1.add(o1);
		G2.add(o2);
		G2.add(o3);
		G3.add(o4);
		G3.add(o5);
		G3.add(o6);
		G4.add(o7);
		G5.add(o8);
		G5.add(o9);
		G6.add(o10);		
		G6.add(o11);
				
		assertTrue(myRdbms.createConstraint("cons1", "PK1", "G1", G1));
		assertTrue(myRdbms.createConstraint("cons1", "AK2", "G2", G2));
		assertTrue(myRdbms.createConstraint("cons2", "PK2", "G3", G3));		
		assertTrue(myRdbms.createConstraint("cons2", "FK1", "G4", G4));
		assertTrue(myRdbms.createConstraint("cons2", "PK2", "G5", G5));			
		assertTrue(myRdbms.createConstraint("cons3", "PK3", "G6", G6));
				
		assertTrue(myRdbms.createReference("REF1", "G4", "G1"));
		assertTrue(myRdbms.createReference("REF2", "G5", "G6"));	
		
		String[] kolom1 = { "nummer", "Naam", "Rel-Date" };
		Object[] value1 = { new Integer(1),new String("Bredevoort"),new Date() };
		assertTrue(myRdbms.insert("Track", kolom1, value1));		
		
		
		String[] kolom3 = { "Track", "Long", "Lat", "Vlgnr" };
		Object[] value3 = { new Integer(1),new Integer(543),new Integer(34),new Integer(900) };
		assertFalse(myRdbms.insert("KnpTrack", kolom3, value3));
		

		String[] kolom2 = { "nummer", "Naam", "Rel-Date" };
		Object[] value2 = { new Integer(2),new String("Groenlo"),new Date() };
		assertTrue(myRdbms.insert("Track", kolom2, value2));
		
		String[] kolom4 = { "Long","Lat",  "Nummer", "Type" };
		Object[] value4 = { new Integer(543),new Integer(34),new Integer(10000),new String("a") };
		assertTrue(myRdbms.insert("Knooppunt", kolom4, value4));
		
		
		assertTrue(myRdbms.insert("KnpTrack", kolom3, value3));
		
		//deze zijn false, vanwege de pk waarde die al bestaat in de tabellen.
		assertFalse(myRdbms.insert("Track", kolom1, value1));
		assertFalse(myRdbms.insert("Track", kolom2, value2));
		assertFalse(myRdbms.insert("Knooppunt", kolom4, value4));
		assertFalse(myRdbms.insert("KnpTrack", kolom3, value3));
		
		
	}

}
